(function($, doc) {
	mui.init({});
	var userinfo = app.getState() || {};
    var uid = userinfo.user_id;
    window.onload = function(){ 	

		mui.plusReady(function(){
			//关闭等待框
			plus.nativeUI.closeWaiting();
			//显示当前页面
			mui.currentWebview.show();
			
			var trade_id=plus.webview.currentWebview().trade_id;
			var froms=plus.webview.currentWebview().froms || {};
			mui.ajax({
				url: apiurl + "Trade/get_trade/",
				data: {'trade_id':trade_id,'_token':userinfo.token},
				async: true,
				dataType: 'json',
				crossDomain: true, //强制使用5+跨域
				type: 'get',
				timeout: 10000,
				success: function(res) {
					console.log(JSON.stringify(res));
					if(res.status ==-3){
						mui.toast(res.msg);
						app.out();
						return false;
					}else {
						// 请求成功
						doc.querySelector("#main-data").innerHTML = template('main-template',res.data);
					}
				},
				error: function(xhr, type, errorThrown) {
					//console.log(JSON.stringify(xhr));
					//console.log(JSON.stringify(type));
					//console.log(JSON.stringify(errorThrown));
					mui.toast('获取数据失败！');
				}
			});
			
			doc.getElementById('buy_record').addEventListener('tap', function() {
				//打开买入记录页
				mui.openWindow({
				    id:'buy_record.html',
				    url:'buy_record.html',
				    show: {
						autoShow:false
					},
					 waiting:{
				       autoShow:true,//自动显示等待框，默认为true
				       title:'正在加载...'//等待对话框上显示的提示内容
				   }
				});
			}) 		


			var old_back = mui.back;
			mui.back = function(){
				    if(froms=='buy_center'){
				    	mui.fire(plus.webview.getWebviewById('buy_center.html'), 'refresh_data');
				    }
			    	//执行mui封装好的窗口关闭逻辑；
			    	old_back();
			}

		});

    }
    
    

}(mui, document));

//取消
function do_cencel(uid,tid){
	var userinfo = app.getState() || {};
	mui.ajax({
		url: apiurl + "Trade/updateTrade/",
		data: {
			'user_id': uid,
			'tid': tid,
			'type': 3,
			'_token':userinfo.token
		},
		async: true,
		dataType: 'json',
		crossDomain: true, //强制使用5+跨域
		type: 'get',
		timeout: 10000,
		success: function(res) {
			console.log(JSON.stringify(res));
			if(res.status ==-3){
				mui.toast(res.msg);
				app.out();
				return false;
			}
			// 请求成功
			if(res.status == 0) {
				mui.alert(res.msg);
			} else {
				mui.toast('取消成功');
				userinfo.a1_money = res.data.a1_money;
				userinfo.a2_money = res.data.a2_money;
				app.setState(userinfo);
				//mui.fire(plus.webview.getLaunchWebview(), 'refresh_amoney');
				mui.fire(plus.webview.getLaunchWebview(), 'refresh_amoney');
				mui.fire(plus.webview.getWebviewById('uncompleted.html'), 'refresh_data');
				mui.fire(plus.webview.getWebviewById('sell_center.html'), 'refresh_data');
				mui.back();
			}
		},
		error: function(xhr, type, errorThrown) {
			//console.log(JSON.stringify(xhr));
			//console.log(JSON.stringify(type));
			//console.log(JSON.stringify(errorThrown));
			mui.toast('操作失败！');
		}
	});
}

//联系他
function do_tel(phone){
	if(mui.os.plus) {
		plus.device.dial(phone);
	} else {
		location.href = 'tel:' + phone;
	}
}


